﻿#region

using System.Collections.Generic;
using Lab.Client.Infrastructure.WcfService;
using Lab.Common.Dto;

#endregion

namespace Client.Presentation {
    public class ClientClass {
        private IClientView view;

        private readonly WcfServiceClient service = new WcfServiceClient();

        private string EmployerName;
        private string EmployerAddress;
        private string EmployerTelephone;
        private string EmployerActivity;

        private string ApplicantName;
        private string ApplicantMidName;
        private string ApplicantSurname;
        private string ApplicantActivity;
        private string ApplicantSkills;
        private string ApplicantResidence;

        private string SkillDescription;

        private string VacancyName;
        private string VacancyOpenDate;
        private string VacancyLocation;
        private string VacancySalary;
        private string VacancySkills;
        private string VacancyActivity;
        private string VacancyEmployer;

        private string DealApplicant;
        private string DealEmployer;
        private string DealVacancy;

        private string ActionMessage;

        private int ListNumber;

        private readonly List<DtoKindOfActivity> listEmployer = new List<DtoKindOfActivity>();

        public ClientClass(IClientView view) {
            Rebind(view);
        }

        public void Rebind(IClientView view) {
            view.OnAddEmployer += OnAddEmployer;
            view.OnAddApplicant += OnAddApplicant;
            view.OnAddSkill += OnAddSkill;
            view.OnAddVacancy += OnAddVacancy;
            view.OnGetApplicant += OnGetApplicant;
            view.OnGetEmployer += OnGetEmployer;
            view.OnGetSkill += OnGetSkill;
            view.OnGetVacancy += OnGetVacancy;
            view.OnMakeDeal += OnMakeDeal;
            view.OnGetDeal += OnGetDeal;
            this.view = view;
        }

        public void OnAddEmployer() {
            EmployerName = view.EmployerName;
            EmployerAddress = view.EmployerAddress;
            EmployerTelephone = view.EmployerTelephone;
            EmployerActivity = view.EmployerActivity;

            ActionMessage = "Adding new Employer...";
            view.ActionMessage = ActionMessage;

            var employer = new DtoEmployer();

            var activity = new DtoKindOfActivity();
            activity.Description = EmployerActivity;
            //var list = new List<DtoKindOfActivity>();
            listEmployer.Add(activity);
            activity.Description = "C#";
            listEmployer.Add(activity);
            employer.KindOfActivities = listEmployer;
            employer.Address = EmployerAddress;
            employer.Name = EmployerName;
            employer.TelephoneNumber = EmployerTelephone;
            service.AddEmployer(employer);
        }

        public void OnGetEmployer() {
            ActionMessage = "Getting all Employers...";
            view.ActionMessage = ActionMessage;
        }

        public void OnAddApplicant() {
            ApplicantName = view.ApplicantName;
            ApplicantMidName = view.ApplicantMidName;
            ApplicantSurname = view.ApplicantSurname;
            ApplicantActivity = view.ApplicantActivity;
            ApplicantSkills = view.ApplicantSkills;
            ApplicantResidence = view.ApplicantResidence;
            ActionMessage = "Adding new Applicant...";
            view.ActionMessage = ActionMessage;
            var applicant = new DtoApplicant();
            applicant.Id = 1;
            var activity = new DtoKindOfActivity();
            activity.Description = ApplicantActivity;
            applicant.KindOfActivity = activity;
            applicant.MiddleName = ApplicantMidName;
            applicant.Name = ApplicantName;
            applicant.PlaceOfResidence = ApplicantResidence;
            var skill = new DtoSkill();
            skill.Description = ApplicantSkills;
            var list = new List<DtoSkill>();
            list.Add(skill);
            applicant.Skills = list;
            applicant.Surname = ApplicantSurname;
            service.AddApplicant(applicant);
        }

        public void OnGetApplicant() {
            ActionMessage = "Getting all Applicants...";
            view.ActionMessage = ActionMessage;
        }

        public void OnAddSkill() {
            SkillDescription = view.SkillsDescripton;
            ActionMessage = "Adding new Skill...";
            view.ActionMessage = ActionMessage;
            var skill = new DtoSkill();
            skill.Description = SkillDescription;
            service.AddSkill(skill);
            ActionMessage = "Skill is added!";
            view.ActionMessage = ActionMessage;
        }

        public void OnGetSkill() {
            ListNumber = view.ListNumber;
            ActionMessage = "Getting all Skills...";
            view.ActionMessage = ActionMessage;
            var skills = service.GetSkills();

            if (ListNumber >= skills.Count) {
                ActionMessage = "OOPS!!! Try to press <- button";
                view.ActionMessage = ActionMessage;
                view.ListNumber = skills.Count;
                return;
            }
            if (ListNumber < 0) {
                ActionMessage = "OOPS!!! Try to press -> button";
                view.ActionMessage = ActionMessage;
                view.ListNumber = 0;
                return;
            }

            if (skills.Count > 0) {
                view.SkillsDescripton = skills[ListNumber].Description;
            }
            else {
                if (skills.Count == 0) {
                    ActionMessage = "OOPS!!! No skill found";
                    view.ActionMessage = ActionMessage;
                    return;
                }
            }
            ActionMessage = "Skills are loaded";
            view.ActionMessage = ActionMessage;
        }

        public void OnAddVacancy() {
            VacancyName = view.VacancyName;
            VacancyOpenDate = view.VacancyOpenDate;
            VacancyLocation = view.VacancyLocation;
            VacancySalary = view.VacancySalary;
            VacancySkills = view.VacancySkills;
            VacancyActivity = view.VacancyActivity;
            VacancyEmployer = view.VacancyEmployer;
            ActionMessage = "Adding new Vacancy...";
            view.ActionMessage = ActionMessage;
            var vacancy = new DtoVacancy();
            var employer = new DtoEmployer();
            var activity = new DtoKindOfActivity();
            activity.Description = VacancyActivity;
            var list = new List<DtoKindOfActivity>();
            list.Add(activity);
            employer.KindOfActivities = list;
            employer.Name = VacancyEmployer;
            var skill = new DtoSkill();
            skill.Description = VacancySkills;
            var skillList = new List<DtoSkill>();
            skillList.Add(skill);
            vacancy.Employer = employer;
            vacancy.ExpectedLevelOfSalary = 40000;
            vacancy.KindOfActivity = activity;
            vacancy.Location = VacancyLocation;
            vacancy.Name = VacancyName;
            vacancy.Skills = skillList;
            service.AddVacancy(vacancy);
        }

        public void OnGetVacancy() {
            ListNumber = view.ListNumber;
            ActionMessage = "Getting new Vacancy...";
            view.ActionMessage = ActionMessage;
            var vacancies = new List<DtoVacancy>();
            vacancies = service.GetOpenVacancies();
            if ((vacancies.Count > 0) && (ListNumber < vacancies.Count)) {
                view.VacancyActivity = vacancies[ListNumber].KindOfActivity.Description;
                view.VacancyEmployer = vacancies[ListNumber].Employer.Name;
                view.VacancyLocation = vacancies[ListNumber].Location;
                view.VacancyName = vacancies[ListNumber].Name;
                view.VacancySalary = vacancies[ListNumber].ExpectedLevelOfSalary.ToString();
                foreach (var skills in vacancies[ListNumber].Skills)
                    view.VacancySkills += skills.Description + " ; ";
            }
            else {
                if (vacancies.Count <= 0)
                    ActionMessage = "OOPS!!! No vacancies found";
                else if (ListNumber >= vacancies.Count)
                    ActionMessage = "OOPS!!! Try to press <- button";
                else if (vacancies.Count < 0)
                    ActionMessage = "OOPS!!! Try to press -> button";
                view.ActionMessage = ActionMessage;
            }
        }

        public void OnMakeDeal() {
            ActionMessage = "Making a Deal...";
            view.ActionMessage = ActionMessage;
            DealApplicant = view.DealApplicant;
            DealEmployer = view.DealEmployer;
            DealVacancy = view.DealVacancy;
            var dtoApplicant = new DtoApplicant();
            dtoApplicant.Surname = DealApplicant;
            var dtoEmployer = new DtoEmployer();
            dtoEmployer.Name = DealEmployer;
            var dtoVacancy = new DtoVacancy();
            dtoVacancy.Name = DealVacancy;
            var dtoDeal = new DtoDeal();
            dtoDeal.Applicant = dtoApplicant;
            dtoDeal.Employer = dtoEmployer;
            dtoDeal.Vacancy = dtoVacancy;
            service.MakeDeal(dtoDeal);
        }

        public void OnGetDeal() {
            ActionMessage = "Getting a Deal...";
            view.ActionMessage = ActionMessage;
        }
    }
}